package fu.ck.Util;


import fu.ck.Main;
import fu.ck.Model.TokenModel;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * Created by xuxingcan on 2016/8/6.
 */
public class MysqlUtil {
    private static volatile MysqlUtil instance;
    private static String MYSQL_URL = "jdbc:mysql://" + Main.MYSQL_URI + "?user=" + Main.MYSQL_USER + "&password=" + Main.MYSQL_PWD + "&useUnicode=true&characterEncoding=UTF8";
    private Connection conn;

    public static MysqlUtil getInstance() {
        if (instance == null) {
            synchronized (MysqlUtil.class) {
                if (instance == null) {
                    instance = new MysqlUtil();
                }
            }
        }
        return instance;
    }

    private MysqlUtil() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(MYSQL_URL);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean unBindUserId(String authCode, String id) {
        if (id == null || id.length() == 0 || authCode == null || authCode.length() == 0) {
            return false;
        }
        try {
            String user_id = "";
            String sql = "select user_id from auth_table where auth_code = '" + authCode + "';";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();// 执行查询
            while (rs.next()) {// 判断是否还有下一个数据
                user_id = rs.getString("user_id");
            }
            rs.close();
            if (user_id == null || user_id.length() == 0) {
                return true;
            } else {
                sql = "update auth_table set user_id = '' where auth_code like '" + authCode + "';";
                ps = conn.prepareStatement(sql);
                ps.execute();
                ps.close();
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean bindUserId(String authCode, String id) {
        if (id == null || id.length() == 0 || authCode == null || authCode.length() == 0) {
            return false;
        }
        try {
            String user_id = "";
            String sql = "select user_id from auth_table where auth_code = '" + authCode + "';";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();// 执行查询
            while (rs.next()) {// 判断是否还有下一个数据
                user_id = rs.getString("user_id");
            }
            rs.close();
            if (user_id == null || user_id.length() == 0) {
                sql = "update auth_table set user_id = '" + id + "' where auth_code like '" + authCode + "';";
                ps = conn.prepareStatement(sql);
                ps.execute();
                ps.close();
                return true;
            } else {
                if (user_id.equalsIgnoreCase(id)) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean isUserIdRight(String authCode, String id) {
        if (id == null || id.length() == 0 || authCode == null || authCode.length() == 0) {
            return false;
        }
        try {
            String sql = "select user_id from auth_table where auth_code = '" + authCode + "';";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();// 执行查询
            String user_id = "";
            while (rs.next()) {// 判断是否还有下一个数据
                user_id = rs.getString("user_id");
            }
            rs.close();
            return user_id.equalsIgnoreCase(id);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public void updateTokenModel(TokenModel tokenModel) {
        try {
            TokenModel oldTokenModel = getTokenModel(tokenModel.user_id);
            if (oldTokenModel == null) {
                String sql = "INSERT INTO token(user_id,token,time) VALUES('" + tokenModel.user_id + "','" + tokenModel.token + "','" + tokenModel.createTime + "');";
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.execute();
            } else {
                String sql = "update token set time = " + tokenModel.createTime + ",token = '" + tokenModel.token + "' where user_id like '" + tokenModel.user_id + "';";
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.execute();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public TokenModel getTokenModel(String id) {
        try {
            TokenModel tokenModel = null;
            String sql = "select * from token where user_id like '" + id + "';";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();// 执行查询
            while (rs.next()) {// 判断是否还有下一个数据
                tokenModel = new TokenModel();
                tokenModel.createTime = rs.getLong("time");
                tokenModel.user_id = rs.getString("user_id");
                tokenModel.token = rs.getString("token");
            }
            return tokenModel;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void heartBeat() {
        try {
            String sql = "select user_id from token where user_id = 'heartBeat';";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();// 执行查询
            rs.close();
//            System.out.println("heartBeat");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
